/*
 * created on Jul 12th 2013
 * you can check clientlib at http://localhost:4502/libs/cq/ui/content/dumplibs.html
 */

console.log('loading the custom composite field clientlib!!');
Ejst.CustomCompositeWidget = CQ.Ext.extend(CQ.form.CompositeField, {
 
    /**
     * @private
     * @type CQ.Ext.form.TextField
     */
    hiddenField: null,
 
    /**
     * @private
     * @type CQ.Ext.form.TextField
     */
    nameField: null,
 
    /**
     * @private
     * @type CQ.Ext.form.pathfield
     */
    linkPathField: null,
   
    constructor: function(config) {
        config = config || { };
        var defaults = {
            "border": false,
            "layout": "table",
            "columns":2
        };
        config = CQ.Util.applyDefaults(config, defaults);
        Ejst.CustomCompositeWidget.superclass.constructor.call(this, config);
    },
 
    // overriding CQ.Ext.Component#initComponent
    initComponent: function() {
        Ejst.CustomCompositeWidget.superclass.initComponent.call(this);
 
        this.hiddenField = new CQ.Ext.form.Hidden({
            name: this.name
        });
        this.add(this.hiddenField);
 
        this.nameField= new CQ.Ext.form.TextField({
            cls:"ejst-customwidget-2",
            listeners: {
                change: {
                    scope:this,
                    fn:this.updateHidden
                }
            }
        });
        this.add(this.nameField);
 
        this.linkPathField= new CQ.form.PathField({
            rootPath: "/content",
            predicate: "nosystem",
            showTitlesInTree: true,
            allowBlank: false,
            listeners: {
                change: {
                    scope:this,
                    fn:this.updateHidden
                },
                dialogclose : {
                    scope:this,
                    fn:this.updateHidden
                }
            }
           
        });
        this.add(this.linkPathField);
 
    },
   
    processInit: function (path, record) {
        this.nameField.processInit(path, record);
        this.linkPathField.processInit(path, record);
    },
 
    // overriding CQ.form.CompositeField#setValue
    setValue: function (value) {
        var parts = value.split("|");
        this.nameField.setValue(parts[0]);
        this.linkPathField.setValue(parts[1]);
        this.hiddenField.setValue(value);
    },
 
    // overriding CQ.form.CompositeField#getValue
    getValue: function() {
        return this.getRawValue();
    },
 
    getRawValue: function () {
        return this.nameField.getValue() + "|" +
               this.linkPathField.getValue();
    },
 
    // private
    updateHidden: function() {
        this.hiddenField.setValue(this.getValue());
    }
 
});
 
// register xtype
CQ.Ext.reg('mypathfield', Ejst.CustomCompositeWidget);